﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    using TestTool.GUI.Utils;
    using TestTool.GUI.Views;
    using TestTool.Tests.Definitions.Enums;
    using TestTool.Tests.Definitions.Features;

    public class FeaturesTree : UserControl, IFeaturesView
    {
        private Dictionary<Feature, TreeNode> _featureNodes = new Dictionary<Feature, TreeNode>();
        private FeaturesSet _featuresSet;
        private bool _inactive;
        private IContainer components;
        private ImageList ilFeaturesIcons;
        private const string NOTSUPPORTEDFEATURE = "NOT SUPPORTED";
        private const string SUPPORTEDFEATURE = "SUPPORTED";
        private TreeViewEx tvFeatures;
        private const string UNDEFINEDFEATURE = "UNDEFINED";

        public event TreeViewCancelEventHandler BeforeSelect;

        public event EventHandler TreeActivated;

        public FeaturesTree()
        {
            this.InitializeComponent();
            this.BuildFeaturesTree();
        }

        private void AddFeatureNode(TreeNode parent, FeatureNode node)
        {
            TreeNode node2 = new TreeNode(node.DisplayName)
            {
                Tag = node,
                Name = node.Name
            };
            if (node.Status != FeatureStatus.Group)
            {
                this.ClearNode(node2);
                this._featureNodes.Add(node.Feature, node2);
            }
            else
            {
                node2.ImageKey = "Group";
                node2.SelectedImageKey = node2.ImageKey;
            }
            if (parent != null)
            {
                parent.Nodes.Add(node2);
            }
            else
            {
                this.tvFeatures.Nodes.Add(node2);
            }
            foreach (FeatureNode node3 in node.Nodes)
            {
                this.AddFeatureNode(node2, node3);
            }
        }

        private void BuildFeaturesTree()
        {
            this._featureNodes.Clear();
            this._featuresSet = FeaturesSet.CreateFeaturesSet();
            FeaturesHelper.Translate(this._featuresSet);
            foreach (FeatureNode node in this._featuresSet.Nodes)
            {
                this.AddFeatureNode(null, node);
            }
            this.tvFeatures.ExpandAll();
            this.tvFeatures.Nodes[0].EnsureVisible();
        }

        public void Clear()
        {
            base.BeginInvoke((Action)delegate
            {
                foreach (TreeNode node in this._featureNodes.Values)
                {
                    if (node.ImageKey != "Group")
                    {
                        this.ClearNode(node);
                    }
                }
            });
        }

        private void ClearNode(TreeNode node)
        {
            node.ImageKey = "Clear";
            node.ToolTipText = "";
            node.SelectedImageKey = node.ImageKey;
        }

        public void DisplayFeature(Feature feature, bool supported)
        {
            base.BeginInvoke((Action)delegate
            {
                if (this._featureNodes.ContainsKey(feature))
                {
                    TreeNode node = this._featureNodes[feature];
                    this.SetupNode(node, supported);
                }
            });
        }

        public void DisplayUndefinedFeature(Feature feature)
        {
            base.BeginInvoke((Action)delegate
            {
                if (this._featureNodes.ContainsKey(feature))
                {
                    TreeNode node = this._featureNodes[feature];
                    node.ImageKey = "UNDEFINED";
                    node.SelectedImageKey = "UNDEFINED";
                }
            });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FeaturesTree));
            this.ilFeaturesIcons = new ImageList(this.components);
            this.tvFeatures = new TreeViewEx();
            base.SuspendLayout();
            this.ilFeaturesIcons.ImageStream = (ImageListStreamer)manager.GetObject("ilFeaturesIcons.ImageStream");
            this.ilFeaturesIcons.TransparentColor = Color.Transparent;
            this.ilFeaturesIcons.Images.SetKeyName(0, "Clear");
            this.ilFeaturesIcons.Images.SetKeyName(1, "Group");
            this.ilFeaturesIcons.Images.SetKeyName(2, "Undefined");
            this.ilFeaturesIcons.Images.SetKeyName(3, "Supported");
            this.ilFeaturesIcons.Images.SetKeyName(4, "NotSupported");
            this.tvFeatures.Dock = DockStyle.Fill;
            this.tvFeatures.ImageIndex = 0;
            this.tvFeatures.ImageList = this.ilFeaturesIcons;
            this.tvFeatures.Location = new Point(0, 0);
            this.tvFeatures.Name = "tvFeatures";
            this.tvFeatures.SelectedImageIndex = 0;
            this.tvFeatures.ShowNodeToolTips = true;
            this.tvFeatures.Size = new Size(0x133, 0x1a3);
            this.tvFeatures.TabIndex = 3;
            this.tvFeatures.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.tvFeatures_NodeMouseClick);
            this.tvFeatures.BeforeSelect += new TreeViewCancelEventHandler(this.tvFeatures_BeforeSelect);
            this.tvFeatures.Click += new EventHandler(this.tvFeatures_Click);
            this.tvFeatures.HideSelection = false;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.tvFeatures);
            base.Name = "FeaturesTree";
            base.Size = new Size(0x133, 0x1a3);
            base.ResumeLayout(false);
        }

        public void SetInactive()
        {
            this._inactive = true;
        }

        private void SetupNode(TreeNode node, bool supported)
        {
            node.ToolTipText = supported ? "SUPPORTED" : "NOT SUPPORTED";
            node.ImageKey = supported ? "Supported" : "NotSupported";
            node.SelectedImageKey = node.ImageKey;
        }

        private void tvFeatures_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (this.BeforeSelect != null)
            {
                this.BeforeSelect(this, e);
            }
        }

        private void tvFeatures_Click(object sender, EventArgs e)
        {
            if (this.TreeActivated != null)
            {
                this.TreeActivated(this, new EventArgs());
            }
        }

        private void tvFeatures_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (this._inactive && (this.TreeActivated != null))
            {
                this.TreeActivated(this, new EventArgs());
            }
        }
    }
}

